package com.x.mvp.widget.dateselector.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;


import com.x.mvp.R;
import com.x.mvp.widget.dateselector.util.CityDataUtil;
import com.x.mvp.widget.dateselector.view.WheelView;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;


/**
 * 日期选择对话框
 * <p/>
 * Created by huangzj on 2015/10/25.
 */
public class SelectAddressDialog extends BaseDialog {

    private View dialogView;
    private WheelView shengWheel;
    private WheelView shiWheel;
    private WheelView xianWheel;
    private static CityDataUtil cityDataUtil;

    int selectYear;
    int selectMonth;

    private OnClickListener onClickListener;

    /**
     * 创建一个日期选择对话框
     *
     * @param mContext
     */
    public SelectAddressDialog(Context mContext) {
        this.context = mContext;
        if (cityDataUtil != null) {
            create();
        }
    }

    public void initData() {
        if (cityDataUtil == null) {
            cityDataUtil = new CityDataUtil();
            cityDataUtil.init(context);
        }
    }

    /**
     * 创建一个日期选择对话框
     *
     * @param mContext
     */
    public SelectAddressDialog(Context mContext, OnClickListener listener) {
        this.context = mContext;
        onClickListener = listener;
        if (cityDataUtil != null) {
            create();
        }
    }

    /**
     * 创建选择日期对话框
     */
    private void create() {

        if (dialog != null) {
            return;
        }
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        dialogView = layoutInflater.inflate(R.layout.dialog_wheel_select_date, null);
        shengWheel = (WheelView) dialogView.findViewById(R.id.select_date_wheel_year_wheel);
        shiWheel = (WheelView) dialogView.findViewById(R.id.select_date_month_wheel);
        xianWheel = (WheelView) dialogView.findViewById(R.id.select_date_day_wheel);

        shengWheel.setWheelItemList(cityDataUtil.getmProvinceDatas());
        shengWheel.setCurrentItem(0);
        shiWheel.setWheelItemList(cityDataUtil.getmCitisDatasMap(shengWheel.getCurrentItemStr()));
        shiWheel.setCurrentItem(0);
        xianWheel.setWheelItemList(cityDataUtil.getmAreaDatasMap(shiWheel.getCurrentItemStr()));
        xianWheel.setCurrentItem(0);

        shengWheel.setOnSelectListener(new WheelView.SelectListener() {
            @Override
            public void onSelect(int index, String text) {
                shiWheel.setWheelItemList(cityDataUtil.getmCitisDatasMap(text));
                if(cityDataUtil.getmCitisDatasMap(text).size() > 0) {
                    shiWheel.setCurrentItem(0);
                }
                xianWheel.setWheelItemList(cityDataUtil.getmAreaDatasMap(shiWheel.getCurrentItemStr()));
            }
        });
        shiWheel.setOnSelectListener(new WheelView.SelectListener() {
            @Override
            public void onSelect(int index, String text) {
                xianWheel.setWheelItemList(cityDataUtil.getmAreaDatasMap(text));
            }
        });

        Button cancelBt = (Button) dialogView.findViewById(R.id.select_date_cancel);
        cancelBt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    if (!onClickListener.onCancel()) {
                        dialog.dismiss();
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
        Button sureBt = (Button) dialogView.findViewById(R.id.select_date_sure);
        sureBt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (onClickListener != null) {
                    if (!onClickListener.onSure(shengWheel.getCurrentItemStr(), shiWheel.getCurrentItemStr(), xianWheel.getCurrentItemStr())) {
                        dialog.dismiss();
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
        dialog = new Dialog(context, R.style.quick_dialog);
        initDialog();
        dialog.setContentView(dialogView);

    }

    protected CompositeSubscription subscriptions = new CompositeSubscription();

    public void addSubscription(Subscription subscription) {
        subscriptions.add(subscription);
    }

    public void unsubscribe() {
        subscriptions.unsubscribe();
    }

    @Override
    public void dismiss() {
        unsubscribe();
        super.dismiss();
    }


    public void showDialog() {
        addSubscription(Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                initData();
                subscriber.onNext("");
                subscriber.onCompleted();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(images -> {
                    create();
                    show();
                }, throwable -> {

                }, () -> {
                }));
    }

    /**
     * 选择日期对话框回调
     *
     * @param listener
     */
    public void setOnClickListener(OnClickListener listener) {
        onClickListener = listener;
    }

    /**
     * 选择日期对话框回调接口，调用者实现
     *
     * @author huangzj
     */
    public interface OnClickListener {
        boolean onSure(String sheng, String shi, String xian);

        boolean onCancel();
    }
}
